package com.albaba.designpattern.visitor.text_001;

import java.text.NumberFormat;
import java.time.LocalDate;

/**
 * @Description: 折扣计价访问者类
 * @Author zx
 * @Date 2025/2/25 18:22
 * @Version 1.0
 */
public class DiscountVisitor implements Visitor {


    private LocalDate billDate;

    public DiscountVisitor(LocalDate billDate) {
        this.billDate = billDate;
        System.out.println("结算日期：" + billDate);
    }


    @Override
    public void visit(Fruit fruit) {
        System.out.println("水果：" + fruit.getName());
        long days = billDate.toEpochDay() - fruit.getProduceDate().toEpochDay();

        double rate = 0;
        if (days > 7) {
            System.out.println("超过7天的水果，禁止售卖");
        } else if (days > 3) {
            rate = 0.5;
        } else {
            rate = 1;
        }

        System.out.println("水果价格：" + NumberFormat.getCurrencyInstance().format(fruit.getPrice() * rate));

    }

    @Override
    public void visit(Wine wine) {
        System.out.println("酒类：" + wine.getName() + "无折扣价格");
        System.out.println("原价售卖：" + NumberFormat.getCurrencyInstance().format(wine.getPrice()));
    }

    @Override
    public void visit(Candy candy) {
        System.out.println("糖果：" + candy.getName());
        // 糖果大于180天，禁止售卖，否则糖果一律九折
        long days = billDate.toEpochDay() - candy.getProduceDate().toEpochDay();

        if (days > 180) {
            System.out.println("糖果已过期，禁止售卖");
        } else {
            System.out.println("九折：" + NumberFormat.getCurrencyInstance().format(candy.getPrice() * 0.9));
        }
    }
}
